home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPPANS.ZIP / FOURFPS.CPP < prev    next >
C/C++ Source or Header  |  1991-07-08  |  1KB  |  66 lines

  1. // fourfps.cpp -- Class with four floating point values
  2.  
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <item.h>
  6.  
  7. class fourFloats : item {
  8.   private:
  9.     double da[3];     // Array of 4 double values
  10.   public:
  11.     fourFloats();
  12.     int setFloat(int n, double x);
  13.     double getFloat(int n);
  14. };
  15.  
  16. void showFloats(fourFloats &ff, const char *s);
  17.  
  18. main()
  19. {
  20.   fourFloats *f4 = new fourFloats;
  21.  
  22.   showFloats(*f4, "\nBefore assignments:\n");
  23.   for (int i = 0; i <= 5; i++)  // Use bad index for test
  24.     f4->setFloat(i, 3.14159 * (i + 1));
  25.   showFloats(*f4, "\nAfter assignments:\n");
  26. }
  27.  
  28. void showFloats(fourFloats &ff, const char *s)
  29. {
  30.   cout << s;
  31.   for (int i = 0; i <= 5; i++)  // Use bad index for test
  32. //    cout << form("value %d: %f\n", i, ff.getFloat(i));
  33.     printf("value %d: %f\n", i, ff.getFloat(i));
  34. }
  35.  
  36. fourFloats::fourFloats()
  37. {
  38.   for (int i = 0; i <= 3; i++)
  39.     da[i] = 0;
  40. }
  41.  
  42. int fourFloats::setFloat(int n, double x)
  43. {
  44.   if ((0 <= n) && (n <= 3)) {
  45.     da[n] = x;
  46.     return 0;   // no error
  47.   } else
  48.     return -1;  // error: n out of range
  49. }
  50.  
  51. double fourFloats::getFloat(int n)
  52. {
  53.   if ((0 <= n) && (n <= 3))
  54.     return da[n];
  55.   else
  56.     return 0;   // bad n values return 0
  57. }
  58.  
  59.  
  60. // Copyright (c) 1990 by Tom Swan. All rights reserved
  61. // Revision 1.00    Date: 12/05/1990   Time: 03:50 pm
  62.  
  63. // Revision 1.01    Date: 07/08/1991   Time: 05:41 pm
  64. // Converted for Borland C++ 2.0
  65.  
  66.